package com.he1618.contoller;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.tencent.polaris.api.config.Configuration;
import com.tencent.polaris.api.core.ProviderAPI;
import com.tencent.polaris.api.rpc.InstanceRegisterRequest;
import com.tencent.polaris.api.rpc.InstanceRegisterResponse;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.factory.ConfigAPIFactory;
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@RestController
public class ServiceController implements InitializingBean {

    private NamingService naming;

    ProviderAPI providerAPI;

    @RequestMapping("/index")
    public ResponseObject<?> index() {
        ResponseObject<Object> result = new ResponseObject<>();


        return result;

    }

    @RequestMapping("/regNacos")
    public ResponseObject<?> regNacos() {
        ResponseObject<Object> result = new ResponseObject<>();

        Instance instance = new Instance();
        instance.setIp("192.168.3.108");
        instance.setPort(2001);
        instance.setHealthy(true);
        instance.setWeight(1.0);
        instance.setEnabled(true);
        Map<String, String> instanceMeta = new HashMap<>();
        instanceMeta.put("preserved.register.source", "SPRING_CLOUD");
        instance.setMetadata(instanceMeta);


        try {
            naming.registerInstance("ims","test", instance);

            naming.subscribe("test", new EventListener() {
                @Override
                public void onEvent(Event event) {
                    System.out.println(((NamingEvent)event).getServiceName());
                    System.out.println(((NamingEvent)event).getInstances());
                }
            });
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }

        return result;

    }

    @RequestMapping("/reg")
    public ResponseObject<?> reg() {
        ResponseObject<Object> result = new ResponseObject<>();

        InstanceRegisterRequest request = new InstanceRegisterRequest();
// 设置实例所属服务信息
        request.setService("boxIms");
// 设置实例所属服务的命名空间信息
        request.setNamespace("boot3");
// 设置实例的 host 信息
        request.setHost("192.168.3.106");
// 设置实例的端口信息
        request.setPort(2001);
// 可选，资源访问Token，即用户/用户组访问凭据，仅当服务端开启客户端鉴权时才需配置
        request.setToken("token");
// 设置实例版本
        request.setVersion("version");
// 设置实例的协议
        request.setProtocol("http");
// 设置实例权重
        request.setWeight(100);
// 设置实例的标签
        // request.setMetadata(metadata);
// 设置实例地理位置 zone 信息
        request.setZone("zone");
// 设置实例地理位置 region 信息
        request.setRegion("region");
// 设置实例地理位置 campus 信息
        request.setCampus("campus");
// 设置心跳健康检查ttl，单位为s，不填默认为5s，TTL的取值范围为 (0s, 60s]
// 开启了心跳健康检查，客户端必须以TTL间隔上报心跳
// 健康检查服务器3个TTL未受到心跳则将实例置为不健康
        request.setTtl(20);
        InstanceRegisterResponse registerResp = providerAPI.registerInstance(request);

        return result;

    }
    @Override
    public void afterPropertiesSet() throws Exception {
        Properties properties = new Properties();
        properties.put("serverAddr", "192.168.3.108:8848");
        properties.put("username", "boxCloud");
        properties.put("password", "he1618");
        properties.put("namespace", "c1a1a27d-d7e7-4d31-be64-c6c985b72ad3");
        properties.put("group", "test");
        naming = NamingFactory.createNamingService(properties);

        Configuration config = ConfigAPIFactory.defaultConfig();
        config.getGlobal().getServerConnector().getAddresses().set(0, "119.91.66.223:8091");
        SDKContext context = SDKContext.initContextByConfig(config);

        providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(context);

    }
}
